package com.tencent.wxcloudrun.frontcontroller;

import com.alibaba.fastjson.JSONObject;
import com.tencent.wxcloudrun.dto.Odersubscription;
import com.tencent.wxcloudrun.dto.Usersubscription;
import com.tencent.wxcloudrun.mapper.OdersubscriptionMapper;
import com.tencent.wxcloudrun.mapper.SubscriptionMapper;
import com.tencent.wxcloudrun.mapper.UsersubscriptionMapper;
import com.tencent.wxcloudrun.service.PayService;
import com.tencent.wxcloudrun.uitls.JwtTokenUtils;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse;
import com.wechat.pay.java.service.payments.nativepay.model.PrepayResponse;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @author lichongbing
 * @version 1.0.0
 * @createdate 2023/4/1 14:21
 * @description: TODO
 */
@RestController
@Api(tags = "订阅")
@CrossOrigin
public class SubscriptionController {

    @Autowired
    private OdersubscriptionMapper odersub;

    @Autowired
    private UsersubscriptionMapper usersubscriptionMapper;

    @Autowired
    private SubscriptionMapper subscriptionMapper;

    @Autowired
    private PayService payService;


    /**
     * 获取用户订阅状态
     */
    @Operation(summary = "获取用户订阅状态")
    @GetMapping("/wxcloud/getSubscriptionStatus")
    public JSONObject getSubscriptionStatus(HttpServletRequest request) {
        String token = request.getHeader("token");
        boolean expiration = JwtTokenUtils.isExpiration(token);
        JSONObject data = new JSONObject();
        JSONObject response = new JSONObject();
        if (expiration == false) {
            String mobile = JwtTokenUtils.getMobile(token);
            //获取用户最新的订阅记录
            Odersubscription odersubscription = odersub.selectList(mobile).get(0);
            //判断用户订阅类型和状态是否有效
            String subscriptionid = odersubscription.getSubscriptionid();
            Usersubscription usersubscription = usersubscriptionMapper.getUsersubscription(subscriptionid, mobile);
            //获取订阅到期时间
            String end = usersubscription.getEnds();
            //获取订阅开始时间
            String start = usersubscription.getStarts();
            subscriptionMapper.getsubscription(subscriptionid);
            //获取订阅名称
            String name = subscriptionMapper.getsubscription(subscriptionid).getName();
            //获取订阅价格
            Integer price = Integer.valueOf(subscriptionMapper.getsubscription(subscriptionid).getPrice());

            data.put("subscriptionid", subscriptionid);
            data.put("end", end);
            data.put("start", start);
            data.put("name", name);
            data.put("price", price / 100);
            data.put("mobile", mobile);
            response.put("data", data);
            response.put("code", 200);
            return response;
        } else {
            response.put("code", 500);
            return response;
        }
    }

    /**
     * 付费订阅下单
     *
     * @param request
     * @param jsonObject
     * @return
     */
    @Operation(summary = "付费订阅下单")
    @PostMapping("/wxcloud/subscriptionOrder")
    public JSONObject subscriptionOrder(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
        String token = request.getHeader("token");
        String mobile = JwtTokenUtils.getMobile(token);
        String subscriptionid = jsonObject.getString("subscriptionid");
        boolean expiration = JwtTokenUtils.isExpiration(token);
        JSONObject data = new JSONObject();
        JSONObject jsonObject1 = new JSONObject();
        if (expiration == false) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
            String start = sdf.format(new Date());
            String price = subscriptionMapper.getsubscription(subscriptionid).getPrice();
            String orderIdByTime = getOrderIdByTime();
            odersub.insertOdersubscription(orderIdByTime, mobile, subscriptionid, start, price);
            data.put("orderid", orderIdByTime);
            data.put("mobile", mobile);
            data.put("subscriptionid", subscriptionid);
            data.put("start", start);
            data.put("price", price);
            //调用支付接口
            PrepayResponse pay = payService.pay(data);
            jsonObject1.put("data", pay);
            jsonObject1.put("code", 200);
            return jsonObject1;
        } else {
            jsonObject1.put("code", 500);
            jsonObject1.put("msg", "token过期");
            return jsonObject1;
        }
    }

    /**
     * 捐款
     * @param jsonObject
     * @return
     */
    @Operation(summary = "捐款")
    @PostMapping("/wxcloud/donate")
    public JSONObject getString(@RequestBody JSONObject jsonObject) {

        JSONObject jsonObject1 = new JSONObject();
        Integer amount = jsonObject.getInteger("amount");
        JSONObject data = new JSONObject();
        String orderIdByTime = getOrderIdByTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String start = sdf.format(new Date());
        data.put("orderid", orderIdByTime);
        data.put("mobile", "1300000000");
        data.put("subscriptionid", "捐款");
        data.put("start", start);
        data.put("price", amount * 100);
        //调用支付接口
        PrepayResponse pay = payService.pay(data);
        jsonObject1.put("data", pay);
        jsonObject1.put("code", 200);
        return jsonObject1;
    }


    /**
     * 生成订单号
     *
     * @return
     */
    private String getOrderIdByTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String newDate = sdf.format(new Date());
        String result = "";
        Random random = new Random();
        for (int i = 0; i < 3; i++) {
            result += random.nextInt(10);
        }
        return newDate + result;
    }

    /**
     * 获取用户订阅历史列表
     *
     * @param request
     * @return
     */
    @Operation(summary = "获取用户订阅历史列表")
    @GetMapping("/wxcloud/getSubscriptionHistory")
    public JSONObject getSubscriptionHistory(HttpServletRequest request) {
        String token = request.getHeader("token");
        boolean expiration = JwtTokenUtils.isExpiration(token);
        JSONObject jsonObject = new JSONObject();
        if (expiration == false) {
            String mobile = JwtTokenUtils.getMobile(token);
            List<Odersubscription> odersubscriptions = odersub.selectList(mobile);
            jsonObject.put("data", odersubscriptions);
            jsonObject.put("code", 200);
            return jsonObject;
        } else {
            jsonObject.put("code", 500);
            return jsonObject;
        }

    }
}
